#!/bin/bash

# Define log file
LOG_FILE="test_script.log"

# Function to log messages to the log file
log_message() {
    echo "\\$1" | tee -a "$LOG_FILE"
}

# File creation test
test_create_file() {
    touch test_file.txt
    if [[ -f test_file.txt ]]; then
        log_message "File created successfully: test_file.txt"
    else
        log_message "File creation failed: test_file.txt"
    fi
}

# File deletion test
test_delete_file() {
    rm -f test_file.txt
    if [[ ! -f test_file.txt ]]; then
        log_message "File deleted successfully: test_file.txt"
    else
        log_message "File deletion failed: test_file.txt"
    fi
}

# File permission modification test
test_modify_file_permissions() {
    touch test_file.txt
    chmod 755 test_file.txt
    perms=$(stat -c "%a" test_file.txt)
    if [[ "$perms" == "755" ]]; then
        log_message "File permission modification successful: test_file.txt"
    else
        log_message "File permission modification failed: test_file.txt"
    fi
    rm -f test_file.txt
}

# Directory creation test
test_create_directory() {
    mkdir test_directory
    if [[ -d test_directory ]]; then
        log_message "Directory created successfully: test_directory"
    else
        log_message "Directory creation failed: test_directory"
    fi
}

# Directory deletion test
test_delete_directory() {
    rmdir test_directory
    if [[ ! -d test_directory ]]; then
        log_message "Directory deleted successfully: test_directory"
    else
        log_message "Directory deletion failed: test_directory"
    fi
}

# Error and exception handling test
test_error_handling() {
    if ! cat non_existent_file.txt &>/dev/null; then
        log_message "Error: Unable to open non-existent file non_existent_file.txt"
    fi
}

# Script starts execution
log_message "Script execution started..."

# Run tests
test_create_file
test_modify_file_permissions
test_delete_file
test_create_directory
test_delete_directory
test_error_handling

# Script execution completed
log_message "Script execution completed."

